Skip to content

esp32: fix linker layout, UART/I2C clocking, and print interleaving - #5617

Closed
flrossetto wants to merge 1 commit into
tinygo-org:devfrom
flrossetto:esp32-classic-fixes
Closed

esp32: fix linker layout, UART/I2C clocking, and print interleaving#5617
flrossetto wants to merge 1 commit into
tinygo-org:devfrom
flrossetto:esp32-classic-fixes

Conversation

@flrossetto

Copy link
Copy Markdown

Found by cross-checking the esp32-generic target against the real esp-idf sources, after confirming the WiFi/BT blob crashes traced back here rather than to the blob itself.

  • targets/esp32.ld: IROM region was sized at a flat 4M instead of the real SOC_IROM_HIGH - SOC_IROM_LOW (0x330000) limit, and the .iram section pattern list was missing (.coexsleepiram), a WiFi/BT-blob section confirmed present via objdump on libcoexist.a. An unmapped section here means the blob's power-management ISR runs out of whatever memory happened to follow, which shows up as sporadic crashes during radio activity.
  • src/machine/machine_esp32.go: UART never set CONF0_TICK_REF_ALWAYS_ON, which only worked by luck on UART0 because the ROM bootloader happens to leave it in APB clock mode; any other UART or a cold path through this code gets the wrong clock source. Also fixes the baud divider to keep its fractional part instead of truncating to a bare integer division.
  • src/machine/machine_esp32_i2c.go: SCL_HIGH_PERIOD compensation was scaled by clock frequency (halfCycle/8) instead of using the fixed offset esp-idf's noise-filter formula actually calls for (thres+6, thres=7 → 13), producing wrong SCL timing at anything other than the frequency it happened to be tested at.
  • src/runtime/scheduler_cooperative.go: printlock/printunlock were no-ops, so two goroutines calling println concurrently can interleave their output mid-line on this cooperative scheduler (print can yield, e.g. waiting on a UART FIFO, before it finishes writing). Added a real bounded-wait lock.

go.mod/go.sum: point tinygo.org/x/espflasher at flrossetto/espflasher (fix-esp32-chip-detect-and-stub-baud-cap) until that fix lands upstream — see that repo for details on the chip-detection and flash baud-rate fixes needed for reliable esp32-generic flashing.

Found by cross-checking the esp32-generic target against the real
esp-idf sources, after confirming the WiFi/BT blob crashes traced
back here rather than to the blob itself.

- targets/esp32.ld: IROM region was sized at a flat 4M instead of the
  real SOC_IROM_HIGH - SOC_IROM_LOW (0x330000) limit, and the .iram
  section pattern list was missing *(.coexsleepiram*), a WiFi/BT-blob
  section confirmed present via objdump on libcoexist.a. An unmapped
  section here means the blob's power-management ISR runs out of
  whatever memory happened to follow, which shows up as sporadic
  crashes during radio activity.
- src/machine/machine_esp32.go: UART never set
  CONF0_TICK_REF_ALWAYS_ON, which only worked by luck on UART0 because
  the ROM bootloader happens to leave it in APB clock mode; any other
  UART or a cold path through this code gets the wrong clock source.
  Also fixes the baud divider to keep its fractional part instead of
  truncating to a bare integer division.
- src/machine/machine_esp32_i2c.go: SCL_HIGH_PERIOD compensation was
  scaled by clock frequency (halfCycle/8) instead of using the fixed
  offset esp-idf's noise-filter formula actually calls for
  (thres+6, thres=7 → 13), producing wrong SCL timing at anything
  other than the frequency it happened to be tested at.
- src/runtime/scheduler_cooperative.go: printlock/printunlock were
  no-ops, so two goroutines calling println concurrently can
  interleave their output mid-line on this cooperative scheduler
  (print can yield, e.g. waiting on a UART FIFO, before it finishes
  writing). Added a real bounded-wait lock.

go.mod/go.sum: point tinygo.org/x/espflasher at flrossetto/espflasher
(fix-esp32-chip-detect-and-stub-baud-cap) until that fix lands
upstream — see that repo for details on the chip-detection and flash
baud-rate fixes needed for reliable esp32-generic flashing.
Comment thread go.mod
tinygo.org/x/go-llvm v0.0.0-20260721072906-185673ef46a5
)

replace tinygo.org/x/espflasher => github.com/flrossetto/espflasher v0.0.0-20260827223712-de27f3043f8f

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not be using a fork here.

@deadprogram

Copy link
Copy Markdown
Member

Please only submit PRs that do a single thing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants